# Contributor: nibon7 <nibon7@163.com>
# Maintainer: nibon7 <nibon7@163.com>
pkgname=v2ray
pkgver=4.45.2
pkgrel=0
pkgdesc="A platform for building proxies to bypass network restrictions"
url="https://v2fly.org"
arch="all"
license="MIT"
options="chmod-clean"
makedepends="go"
subpackages="$pkgname-openrc"
_geosite_ver=20220612032845
_geoip_ver=202206090051
source="$pkgname-core-$pkgver.tar.gz::https://github.com/v2fly/v2ray-core/archive/v$pkgver.tar.gz
	geosite-$_geosite_ver.dat::https://github.com/v2fly/domain-list-community/releases/download/$_geosite_ver/dlc.dat
	geoip-$_geoip_ver.dat::https://github.com/v2fly/geoip/releases/download/$_geoip_ver/geoip.dat
	v2ray.initd
	0001-fix-readRequest-API-changed-since-go1.17-1370.patch
	"

builddir="$srcdir/$pkgname-core-$pkgver"

export CGO_ENABLED=0

build() {
	local ldflags="
		-X github.com/v2fly/v2ray-core/v4.codename=$pkgname
		-X github.com/v2fly/v2ray-core/v4.version=$pkgver
		-X github.com/v2fly/v2ray-core/v4.build=$SOURCE_DATE_EPOCH
		-s -w -buildid=
		"
	go build -trimpath -ldflags "$ldflags" -o v2ray ./main
	go build -trimpath -ldflags "$ldflags" -tags confonly -o v2ctl \
		./infra/control/main
}

check() {
	# testing/sceanarios may fail
	# app/{stats, router},  transport/{pipe, internet/websocket}, proxy/vmess, common/signal fail on armv7
	# app/router/command, app/dns fail on aarch64
	# transport/internet/quic, common/task fail on s390x
	# v4/infra/conf/{rule, geodata/memconservative} may fail
	# v4/transport/internet/kcp may fail
	local pkgs=$(go list ./... | grep -v \
		-e 'app/dns$' \
		-e 'app/stats$' \
		-e 'app/router$' \
		-e 'app/router/command$' \
		-e 'common/task$' \
		-e 'common/signal$' \
		-e 'testing/scenarios$' \
		-e 'proxy/vmess$' \
		-e 'transport/pipe$' \
		-e 'transport/internet/websocket$' \
		-e 'transport/internet/quic$' \
		-e 'v4/infra/conf/rule$' \
		-e 'v4/infra/conf/geodata/memconservative$' \
		-e 'v4/transport/internet/kcp$' \
		)
	go test $pkgs
}

package() {
	install -Dm755 -d "$pkgdir"/etc/$pkgname
	install -m644 release/config/*.json -t "$pkgdir"/etc/$pkgname

	install -Dm755 -d "$pkgdir"/usr/share/$pkgname
	install -m644 "$srcdir"/geosite-$_geosite_ver.dat \
		"$pkgdir"/usr/share/$pkgname/geosite.dat
	install -m644 "$srcdir"/geoip-$_geoip_ver.dat \
		"$pkgdir"/usr/share/$pkgname/geoip.dat

	install -Dm755 v2ray "$pkgdir"/usr/bin/v2ray
	install -Dm755 v2ctl "$pkgdir"/usr/bin/v2ctl

	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
}

sha512sums="
78805d1fe4b1153dd654e61b4d2e3fedf6c5055e69ce4b86bfae1214904eca2da09595dc0d6b5873ad06e4a6f91a061eaae1f75e363e851693a50b0c26ad25e2  v2ray-core-4.45.2.tar.gz
faedae16037f15b4f9893456431f384252cc1b6a5c51fce6a3491a34643c3ac67c5638a8ed4103ccd62079158ff8c5eb6db1df7c7a576e61fad0806ee28eb6fe  geosite-20220612032845.dat
ef5941d147e1493ed7dcb8f5af3eb64449d8e97f5662472690ad148843c126c83adaeb398d4d4fb9c2435d3e201fa7e034345927695033c3579a36f1eb565d44  geoip-202206090051.dat
f22cd2ef4182a003a4e0f94a550e5805c6c30d608d9e655c98e850b5fd8b3785c30df9a8e07c2d5460e6dac9088316f901018a89143e47c9da168e3aee3b94ca  v2ray.initd
d5d0d6f2dde30e1b97352fdbe46fa3c75797963d72648778345bd6bbb844b77f1304be26461678a07d7d654401376bca126aca26da76deb8280dc419c0984c0c  0001-fix-readRequest-API-changed-since-go1.17-1370.patch
"
